clear all
use ../processed/NLSY79.dta, replace

gen AFQT3_a = max(AFQT3-1/3,0)
gen AFQT3_b = max(AFQT3-2/3,0)
gen age_a = max(age-35,0)
gen age_b = max(age-45,0)
gen male = (sex==1)
gen female = (sex==2)

local ctrl AFQT3 AFQT3_a AFQT3_b age age_a age_b female blk hsp nsibs locE locU urban i.hgc_m i.hgc_f i.yob i.divisiona
local iv pub4 tui4 locE17 locU17

local xlist
forvalues j=9/18{
	gen x`j' = (hgc_r>=`j')
	local xlist `xlist' x`j'
}

reg hgc_r `ctrl' [aw=wgt]
predict xres, resid
predict qhat, xb
reg hgc_r `iv' `ctrl' [aw=wgt]
predict xhat, xb
reg xhat `ctrl' [aw=wgt]
predict zres, resid

xtile qhat_q = qhat [aw=wgt], nq(3)
gen AFQT_q = 1
replace AFQT_q = 2 if AFQT3>(1/3)
replace AFQT_q = 3 if AFQT3>(2/3)
gen age_q = 1
replace age_q = 2 if age>=35
replace age_q = 3 if age>=45
xtile locwage_q = locE [aw=wgt], nq(3)
xtile yob_q = yob [aw=wgt], nq(4)
gen hgc_p = max(hgc_m,hgc_f)
tab hgc_p, m
gen hgc_p1 = (hgc_p<=2) // neither graduates from high school
gen hgc_p2 = (hgc_p==3) // at least one graduates from high school but neither attend college
gen hgc_p3 = (hgc_p>=4) // at least one attends college
gen nbh = (blk==0 & hsp==0)
gen rural = (1-urban)
gen r1 = ( divisiona>=1 & divisiona<=2 ) // Northeast
gen r2 = ( divisiona>=3 & divisiona<=4 ) // Midwest
gen r3 = ( divisiona>=5 & divisiona<=7 ) // South
gen r4 = ( divisiona>=8 & divisiona<=9 ) // West
gen ns1 = ( nsibs<=2 )
gen ns2 = ( nsibs==3 | nsibs==4 )
gen ns3 = ( nsibs==5 | nsibs==6 )
local wlist male female blk hsp nbh rural urban hgc_p1 hgc_p2 hgc_p3 r1 r2 r3 r4 ns1 ns2 ns3
forvalues j=1/3{
	foreach v in AFQT age locwage qhat{
		gen `v'_q`j' = (`v'_q==`j')
		local wlist `wlist' `v'_q`j'
	}
}
forvalues j=1/4{
	foreach v in yob{
		gen `v'_q`j' = (`v'_q==`j')
		local wlist `wlist' `v'_q`j'
	}
}
tabstat `wlist' [aw=wgt], stat(mean)
local nv = 0
foreach v in `wlist'{
	local nv = `nv'+1
}
matrix A = J(`nv',8,.)
mat rownames A = `wlist'
mat colnames A = w_pop w_pop_se w_ols w_ols_se w_iv w_iv_se b_ols b_ols_se
// OLS and IV weight on group of covariates
local j=0
qui foreach v in `wlist'{
	local j=`j'+1
	gen w_`v' = `v'*xres
	reg `v' [pw=wgt]
	matrix A[`j',1] =  _b[_cons]
	matrix A[`j',2] = _se[_cons]	
	regress w_`v' xres [pw=wgt], vce(cluster cty_grp) nocons
	matrix A[`j',3] =  _b[xres]
	matrix A[`j',4] = _se[xres]
	ivregress 2sls w_`v' (xres=zres) [pw=wgt], vce(cluster cty_grp) nocons
	matrix A[`j',5] =  _b[xres]
	matrix A[`j',6] = _se[xres]	
	reg logwage hgc_r `ctrl' [pw=wgt] if `v'==1, vce(cluster cty_grp) 
	matrix A[`j',7] =  _b[hgc_r]
	matrix A[`j',8] = _se[hgc_r]	
}
preserve
clear
svmat A, names(col)
gen grp = "a"
local i=0
foreach v in `wlist'{
	local i = `i'+1
	replace grp = `"`v'"' in `i'
}
order grp, first
export delimited using ../result/wgt_w.csv, replace
restore

matrix B = J(11,8,.)
mat colnames B = w_pop w_pop_se w_ols w_ols_se w_iv w_iv_se b_ols b_ols_se
// OLS and IV weight on x
gen d8 = hgc_r==8
qui reg d8 [pw=wgt]
matrix B[1,1] =  _b[_cons]
matrix B[1,2] = _se[_cons]
qui forvalues j=9/18{
	gen d`j' = hgc_r==`j'
	qui reg d`j' [pw=wgt]
	matrix B[`j'-7,1] =  _b[_cons]
	matrix B[`j'-7,2] = _se[_cons]	
	reg x`j' hgc_r `ctrl' [pw=wgt], vce(cluster cty_grp)
	matrix B[`j'-7,3] =  _b[hgc_r]
	matrix B[`j'-7,4] = _se[hgc_r]	
	ivregress 2sls x`j' (hgc_r=`iv') `ctrl' [pw=wgt], vce(cluster cty_grp)
	matrix B[`j'-7,5] =  _b[hgc_r]
	matrix B[`j'-7,6] = _se[hgc_r]	
	reg logwage hgc_r `ctrl' [pw=wgt] if hgc_r==`j'|hgc_r==`j'-1, vce(cluster cty_grp)
	matrix B[`j'-7,7] =  _b[hgc_r]
	matrix B[`j'-7,8] = _se[hgc_r]	
}
preserve
clear
svmat B, names(col)
gen x = _n+7
order x, first
export delimited using ../result/wgt_x.csv, replace
restore
